import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { db } from '@/lib/db';
// GET /api/accounts/[id] - Get account by ID (user-specific)
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions);
if (!session || !session.user) {
return NextResponse.json(
{ success: false, error: 'Unauthorized' },
{ status: 401 }
);
}
const accountId = parseInt(params.id);
if (isNaN(accountId)) {
return NextResponse.json(
{ success: false, error: 'Invalid account ID' },
{ status: 400 }
);
}
const userId = parseInt((session.user as any).id);
const account = await db.getAccountById(accountId, userId);
if (!account) {
return NextResponse.json(
{ success: false, error: 'Account not found' },
{ status: 404 }
);
}
return NextResponse.json({ success: true, account });
} catch (error: any) {
console.error('Error fetching account:', error);
return NextResponse.json(
{ success: false, error: error.message },
{ status: 500 }
);
}
}
// PUT /api/accounts/[id] - Update account (user-specific)
export async function PUT(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions);
if (!session || !session.user) {
return NextResponse.json(
{ success: false, error: 'Unauthorized' },
{ status: 401 }
);
}
const accountId = parseInt(params.id);
if (isNaN(accountId)) {
return NextResponse.json(
{ success: false, error: 'Invalid account ID' },
{ status: 400 }
);
}
const body = await request.json();
const { name, broker_id, api_key, api_secret } = body;
if (!name) {
return NextResponse.json(
{ success: false, error: 'Name is required' },
{ status: 400 }
);
}
const userId = parseInt((session.user as any).id);
const affectedRows = await db.updateAccount(accountId, userId, name, broker_id, api_key, api_secret);
if (affectedRows === 0) {
return NextResponse.json(
{ success: false, error: 'Account not found' },
{ status: 404 }
);
}
const account = await db.getAccountById(accountId, userId);
return NextResponse.json({ success: true, account });
} catch (error: any) {
console.error('Error updating account:', error);
return NextResponse.json(
{ success: false, error: error.message },
{ status: 500 }
);
}
}
// DELETE /api/accounts/[id] - Delete account (user-specific)
export async function DELETE(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions);
if (!session || !session.user) {
return NextResponse.json(
{ success: false, error: 'Unauthorized' },
{ status: 401 }
);
}
const accountId = parseInt(params.id);
if (isNaN(accountId)) {
return NextResponse.json(
{ success: false, error: 'Invalid account ID' },
{ status: 400 }
);
}
const userId = parseInt((session.user as any).id);
const affectedRows = await db.deleteAccount(accountId, userId);
if (affectedRows === 0) {
return NextResponse.json(
{ success: false, error: 'Account not found' },
{ status: 404 }
);
}
return NextResponse.json({
success: true,
message: 'Account deleted successfully'
});
} catch (error: any) {
console.error('Error deleting account:', error);
return NextResponse.json(
{ success: false, error: error.message },
{ status: 500 }
);
}
}